package com.xuanyang.dormrepair.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.xuanyang.dormrepair.config.hook.CorsFilter;
import com.xuanyang.dormrepair.config.hook.LoginInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @AUTHOR soft
 * @DATE 2019/5/19 1:35
 * @DESCRIBE 所有的配置都在这里
 */
@Configuration
@MapperScan("com.xuanyang.dormrepair.model.mapper")
@EnableTransactionManagement(proxyTargetClass = true)
public class AllConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * mybatis plus 分页插件
     */
    @Bean
    public PaginationInterceptor pageInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 注册一个跨域拦截器
     */
    @Bean
    public FilterRegistrationBean filters() {
        CorsFilter filter = new CorsFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean();

        registration.setFilter(filter);
        registration.addUrlPatterns("/api/*");
        registration.setName("CorsFilter");
        registration.setOrder(1);
        return registration;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**");

        super.addInterceptors(registry);
    }
}
